package com.wuzuqing.android.eightdiagrams.utils;

import android.content.Context;
import android.net.wifi.WifiManager;

/**
 * @CopyRight: 广州仰望星空云科技有限公司
 * @Author : wuzuqing
 * @Email:wuzuqing@xinyu668.com
 * @CreateTime: 19.8.25 4:47 PM
 * @Description: 请写描述
 */
public class Test {

    public static String getWifiIpAddress(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (!wifiManager.isWifiEnabled()) {
            // Wi-Fi is not enabled, return null or handle the case accordingly
            return null;
        }

        int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; ++i) {
            bytes[i] = (byte)(ipAddress >> ((3 - i) * 8));
        }

        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append((b & 0xFF)).append(".");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }
}
